﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: Propietario			*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.Entity;
using System.Data;
using ESF.General;
using ESF.SSO.Core.Entities;
using ESF.SSO.Core.Entities.BasicReferences;
using ESF.SSO.Core.DataAccess.BasicReferences;
using System.Collections.Specialized;
using ESF.General.Text;

#endregion


namespace ESF.SSO.Core.DataAccess
{
	public static class AdoUser
	{
		#region Custom Lists

		public static List<string> ListUserEnterprises()
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetUserEnterpriseNames");

			return AdoESFSSO.R.GetListStringVector (cmd);
		}

		#endregion


		#region Read Values
		
		public static List<BasicReference> GetUserBasicReferenceValues (User pUser, BasicReferenceTypes pBasicReferenceType, bool? pOnlyByIsEnabled = null)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetUserBasicReferenceValues");
			
			AdoESFSSO.R.AddParameter (cmd, "pUserAlias", pUser.Alias);
			AdoESFSSO.R.AddIDParameter (cmd, "pBasicReferenceTypeID", (int) pBasicReferenceType);
			AdoESFSSO.R.AddParameter (cmd, "pOnlyByIsEnabled", pOnlyByIsEnabled);
			
			return AdoESFSSO.R.GetList <BasicReference> (cmd, ConvertToBasicReference);
		}


		public static BasicReference GetUserBasicReferenceValues (User pUser, BasicReference pRef)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetUserBasicReferenceValues");
			
			AdoESFSSO.R.AddParameter (cmd, "pUserAlias", pUser.Alias);
			AdoESFSSO.R.AddIDParameter (cmd, "pBasicReferenceTypeID", pRef.BasicReferenceType.ID);
			AdoESFSSO.R.AddCodeParameter (cmd, "pBasicReferenceCode", pRef);
			
			return AdoESFSSO.R.Get <BasicReference> (cmd, ConvertToBasicReference);
		}
		
		
		public static void DeleteUserBasicReferenceValues (User pUser, BasicReferenceTypes pBasicReferenceType)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_DeleteUserBasicReferenceValues");
			
			AdoESFSSO.R.AddParameter (cmd, "pUserAlias", pUser.Alias);
			AdoESFSSO.W.AddIDParameter (cmd, "pBasicReferenceTypeID", (int) pBasicReferenceType);
		
			AdoESFSSO.W.Execute (cmd);
		}	
		
		
		public static void SetUserBasicReferenceValue (User pUser, BasicReference pValue)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_SetUserBasicReferenceValue");
			
			AdoESFSSO.W.AddIDParameter (cmd, "pUserPersonID", pUser.ID);
			AdoESFSSO.W.AddIDParameter (cmd, "pBasicReferenceID", pValue.ID);
			AdoESFSSO.W.AddParameter (cmd, "pValue", pValue.Value);
			
			AdoESFSSO.W.Execute (cmd);
		}
		
		#endregion
		
		
		#region Read


		public static List<string> GetAllEmails(bool? pUserIsEnabled)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetAllEmails");

			AdoESFSSO.R.AddParameter (cmd, "pIsEnabled", pUserIsEnabled);

			return AdoESFSSO.R.GetListStringVector (cmd);
		}


		public static List<string> ListUserAlias(string pAliasPart, int pCount)
		{
			IDbCommand cmd;

			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_ListUserAlias");

			AdoESFSSO.R.AddParameter (cmd, "pAliasPart", pAliasPart, ComparisonTypes.LikeFull);

			return DataHelper.GetStringVector (AdoESFSSO.R.GetFirstDataTable (cmd));
		}


		public static BrowseResults<User> Browse (User pSample, PaggingInfo pPagInfo)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_BrowseUsers");
			
			AdoESFSSO.R.AddParameter (cmd, "pAlias", pSample.Alias, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pFirstName", pSample.FirstName, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pSecondName", pSample.SecondName, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pSurName", pSample.SurName, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pLastName", pSample.LastName, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pFullName", pSample.filFullName, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter(cmd, "pEasySearch", pSample.filEasySearch);

			AdoESFSSO.R.AddNameParameter (cmd, "pEnterpriseName", pSample.Enterprise, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pProfession", pSample.Profession, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pPosition", pSample.Position, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddIDParameter (cmd, "pGenderID", pSample.Gender);
			AdoESFSSO.R.AddIDParameter (cmd, "pIdentificationTypeID", pSample.IdentificationType);
			
			if (!DataHelper.IsEmpty (pSample.UserGroups))
			{
				AdoESFSSO.R.AddIDParameter (cmd, "pUserGroupID", pSample.UserGroups.First ());
			}

			AdoESFSSO.R.AddParameter (cmd, "pIdentificationNumber", pSample.IdentificationNumber, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pEmail", pSample.Email, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter(cmd, "pExtraValue", pSample.filExtraValue);
			
			AdoESFSSO.R.AddParameter (cmd, "pIsEnabled", pSample.IsEnabled);
			
			AdoESFSSO.R.AddParametersPagging (cmd, pPagInfo);
			
			
			return AdoESFSSO.R.GetBrowse<User> (cmd, ConvertTo, pPagInfo);
		}


		public static List<UserMenuItem> GetUserApplicationList(User pUser)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetUserApplicationList");		
		
			if (pUser != null)
			{
				AdoESFSSO.R.AddIDParameter (cmd, "pPersonID", pUser.ID);
			}
			
			return AdoESFSSO.R.GetList<UserMenuItem> (cmd, DataHelper.ConvertToUserMenuItem);
		}
		
		
		public static User GetUserFromSessionID(string pSessionID)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetUserFromSessionID");
			
			AdoESFSSO.W.AddParameter (cmd, "pSessionID", pSessionID);
			
			return AdoESFSSO.R.Get <User> (cmd, ConvertTo);
		}
		
		
		public static User GetUserByIdentificationCredentials (User pUser)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetUserByIdentificationCredentials");
			
			AdoESFSSO.R.AddParameter (cmd, "pIdentificationTypeID", pUser.IdentificationType.ID);
			AdoESFSSO.R.AddParameter (cmd, "pIdentificationNumber", pUser.IdentificationNumber);
			AdoESFSSO.R.AddParameter (cmd, "pPassword", pUser.Password);
			
			return AdoESFSSO.R.Get<User> (cmd, ConvertTo);
		}
		
		
		public static User GetUserByCredentials (User pUser)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_GetUserByCredentials");
			
			AdoESFSSO.W.AddParameter (cmd, "pAlias", pUser.Alias);
			AdoESFSSO.W.AddParameter (cmd, "pPassword", pUser.Password);
			
			return AdoESFSSO.R.Get <User> (cmd, ConvertTo);
		}
		
		
		public static bool IsThereUserByIdentificationCredentials (User pUser)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_IsThereUserByIdentificationCredentials");
			
			AdoESFSSO.W.AddParameter (cmd, "pIdentificationTypeID", pUser.IdentificationType.ID);
			AdoESFSSO.W.AddParameter (cmd, "pIdentificationNumber", pUser.IdentificationNumber);
			AdoESFSSO.W.AddParameter (cmd, "pPassword", pUser.Password);
			
			return AdoESFSSO.R.GetBool (cmd);
		}
		
		
		public static bool IsThereUserByCredentials (User pUser)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_IsThereUserByCredentials");
			
			AdoESFSSO.W.AddParameter (cmd, "pAlias", pUser.Alias);
			AdoESFSSO.W.AddParameter (cmd, "pPassword", pUser.Password);
			
			return AdoESFSSO.R.GetBool (cmd);
		}
		
		
		public static User ConvertTo (IDataReader pReader, string pPrefix = "", string pListLoading = "")
		{
			User user;
			
			
			user = DataHelper.ConvertToAuditableEntity <User> (pReader, pPrefix + "Usr_");
			
			user.ID = DataHelper.GetNullableInt (pReader, pPrefix + "Usr_Per_ID");
			user.Alias = DataHelper.GetString (pReader, pPrefix + "Usr_Alias");
			
			user.FirstName = DataHelper.GetString (pReader, pPrefix + "Usr_Per_FirstName");
			user.SecondName = DataHelper.GetString (pReader, pPrefix + "Usr_Per_SecondName");
			user.SurName = DataHelper.GetString (pReader, pPrefix + "Usr_Per_SurName");
			user.LastName = DataHelper.GetString (pReader, pPrefix + "Usr_Per_LastName");
			user.Email = DataHelper.GetString (pReader, pPrefix + "Usr_Per_Email");
			
			user.IdentificationNumber = DataHelper.GetString (pReader, pPrefix + "Usr_Per_IdentificationNumber");
			user.SecurityQuestionAnswer = DataHelper.GetString (pReader, "Usr_SecurityQuestionAnswer");
			user.IsEnabled = DataHelper.GetNullableBool (pReader, "Usr_IsEnabled");
			user.BirthDate = DataHelper.GetNullableDateTime (pReader, "Usr_Per_BirthDate");
			
			user.Password = DataHelper.GetString (pReader, "Usr_Password");

			user.SecurityQuestion = DataHelper.ConvertToBasicReference<BasicReference> (pReader, "Usr_Sqa_");
			user.IdentificationType = DataHelper.ConvertToBasicReference<BasicReference> (pReader, "Usr_Per_IT_");
			user.Gender = DataHelper.ConvertToBasicReference<BasicReference> (pReader, "Usr_Per_Gen_");
			
			user.Profession = DataHelper.GetString (pReader, "Usr_Per_Profession");
			user.Position = DataHelper.GetString (pReader, "Usr_Per_Position");

			user.Enterprise = AdoEnterprise.ConvertTo(pReader, pPrefix + "Usr_Per_");

			user.extEnt = DataHelper.ConvertToDirectBasicReference(pReader, pPrefix + "Usr_extEnt_");

			user.Locations = AdoLocation.GetList (user).ToList ();
			
			user.UserGroups = GetUserGroups (user).ToList ();
			user.UserRights = GetUserBasicReferenceValues (user, BasicReferenceTypes.UserRights, true).ToList ();

			return user;
		}
		
		
		public static BasicReference ConvertToBasicReference (IDataReader pReader, string pPrefix = "", string pListLoading = "")
		{
			BasicReference result;
			
			
			result = DataHelper.ConvertToBasicReference <BasicReference> (pReader);
			
			result.Value = DataHelper.GetString (pReader, pPrefix + "Value");
			
			return result;
		}


		public static List<BasicReference> GetUserGroups(User pUser)
		{
			return GetBasicReferences (pUser, BasicReferenceTypes.UserGroup);
		}


		public static List<BasicReference> GetBasicReferences(User pUser, BasicReferenceTypes pBasicReferenceType)
		{
			return GetBasicReferences (pUser, (int) pBasicReferenceType);
		}


		public static BasicReference GetFirstBasicReference (User pUser, int pBasicReferenceTypeID)
		{
			List<BasicReference> references;


			references = GetBasicReferences (pUser, pBasicReferenceTypeID);

			if (DataHelper.IsEmpty (references)) { return null; }

			return references.First ();
		}


		public static List<BasicReference> GetBasicReferences(User pUser, int pBasicReferenceTypeID)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetUser_BasicReferences");
			
			AdoESFSSO.R.AddIDParameter (cmd, "pUserPersonID", pUser.ID);
			AdoESFSSO.R.AddIDParameter (cmd, "pBasicReferenceTypeID", pBasicReferenceTypeID);
			
			return AdoESFSSO.R.GetList<BasicReference> (cmd, DataHelper.ConvertToBasicReference<BasicReference>);
		}


		public static List<UserMenuItem> GetUserMenus(BasicReference pApplication, BasicReference pUserGroup, BasicReference pParentUserMenu)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetUserMenus");

			AdoESFSSO.R.AddIDParameter (cmd, "pApplicationID", pApplication);
			AdoESFSSO.R.AddIDParameter (cmd, "pUserGroupID", pUserGroup);
			AdoESFSSO.R.AddIDParameter (cmd, "pParentUserMenuID", pParentUserMenu);

			return AdoESFSSO.R.GetList <UserMenuItem> (cmd, DataHelper.ConvertToUserMenuItem);
		}


		public static UserMenuItem GetUserMenu (UserMenuItem pUserMenuItem)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetUserMenu");

			AdoESFSSO.R.AddIDParameter (cmd, "pID", pUserMenuItem);

			return AdoESFSSO.R.Get<UserMenuItem> (cmd, ConvertToUserMenuItem);
		}


		private static UserMenuItem ConvertToUserMenuItem (IDataReader pReader, string pPrefix = "", string pListLoading = "")
		{
			UserMenuItem item;
			
			
			item = DataHelper.ConvertToUserMenuItem (pReader, pPrefix);

			//item.Files = AdoBasicReference.GetFiles (item).ToList ();

			return item;
		}


		public static List<UserMenuItem> GetUserMenu(User pUser, BasicReference pApplication)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetUserMenusForUser");
			
			if (pUser != null)
			{
				AdoESFSSO.R.AddIDParameter (cmd, "pPersonID", pUser.ID);
			}
			
			AdoESFSSO.R.AddCodeParameter (cmd, "pApplicationCode", pApplication);
			
			return AdoESFSSO.R.GetList<UserMenuItem> (cmd, DataHelper.ConvertToUserMenuItem);
		}


		//public static List<FileReference> GetUserMenuImages(User pUser, BasicReference pApplication)
		//{
		//    IDbCommand cmd;
			
			
		//    cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetUserMenuImagesForUser");
			
		//    if (pUser != null)
		//    {
		//        AdoESFSSO.R.AddIDParameter (cmd, "pPersonID", pUser.ID);
		//    }
			
		//    AdoESFSSO.R.AddCodeParameter (cmd, "pApplicationCode", pApplication);
			
		//    return new List<FileReference> ();
		//    //return AdoESFSSO.R.GetList<FileReference> (cmd, DataHelper.ConvertToFileReference);
		//}
		
		
		public static User Get (User pUser)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetUser");
			
			AdoESFSSO.R.AddIDParameter (cmd, "pPersonID", pUser.ID);
			AdoESFSSO.R.AddParameter (cmd, "pAlias", pUser.Alias);
			
			
			return AdoESFSSO.R.Get <User> (cmd, ConvertTo);
		}
		
		#endregion
		
		
		#region Write
		
		public static void Delete (User pUser)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_DeleteUser");
			
			AdoESFSSO.W.AddIDParameter (cmd, "pPersonID", pUser.ID);
			
			AdoESFSSO.W.Execute (cmd);
		}
		
		
		public static void SetPassword (User pUser)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("[esf_sso].[P_ResetUserPassword]");
			
			AdoESFSSO.W.AddParameter (cmd, "pPassword", pUser.Password);
			AdoESFSSO.W.AddParameter (cmd, "pAlias", pUser.Alias);
			AdoESFSSO.W.AddAuditableEntityParamsUpdate (cmd, pUser);
			
			AdoESFSSO.W.Execute (cmd);
		}
		
		
		public static User RememberPassword (User pUser)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("[esf_sso].[P_GetUserPassword]");
			
			AdoESFSSO.R.AddParameter (cmd, "pEmail", pUser.Email);
			AdoESFSSO.R.AddParameter (cmd, "pSecurityQuestionID", pUser.SecurityQuestion.ID);
			AdoESFSSO.R.AddParameter (cmd, "pSecurityQuestionAnswer", pUser.SecurityQuestionAnswer);
			
			return AdoESFSSO.R.Get<User> (cmd, ConvertTo);
		}
		
		
		
		private static void AdjustEnterprise (ref User pUser)
		{
			if (!Validate.IsFilledID (pUser.Enterprise))
			{
				if (TextHelper.IsEmpty (pUser.Enterprise.Name))
				{
					pUser.Enterprise = null;
				}
				else
				{
					pUser.Enterprise = AdoEnterprise.GetByName (new Enterprise () { Name = pUser.Enterprise.Name });
				}
			}
		}
		
		
		public static void Register (ref User pUser)
		{
			IDbCommand cmd;
			
			NameValueCollection dbClientMsg;
			
			
			dbClientMsg = new NameValueCollection ();
			dbClientMsg.Add ("U_User_Alias", string.Format ("El nombre de usuario {0} ya esta asignado cambielo e intente de nuevo", pUser.Alias));
			
			
			if (pUser.ID == null)
			{
				pUser.ID = AdoPerson.Register (pUser);
			
				cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_InsertUser");
				
				AdoESFSSO.W.AddIDParameter (cmd, "pPersonID", pUser.ID);
				AdoESFSSO.W.AddParameter (cmd, "pAlias", pUser.Alias);
				AdoESFSSO.W.AddParameter (cmd, "pPassword", pUser.Password);
				AdoESFSSO.W.AddParameter (cmd, "pIsEnabled", pUser.IsEnabled);
				AdoESFSSO.W.AddIDParameter (cmd, "pSecurityQuestionID", pUser.SecurityQuestion.ID);
				AdoESFSSO.W.AddParameter (cmd, "pSecurityQuestionAnswer", pUser.SecurityQuestionAnswer);
				AdoESFSSO.W.AddAuditableEntityParamsCreation (cmd, pUser);
				
				AdoESFSSO.W.Execute (cmd, dbClientMsg);
			}
			else
			{
				AdoPerson.Register (pUser);
			
				// Solo si viene diligenciada la contraseña
				if (!string.IsNullOrEmpty (pUser.Password))
				{
					cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_UpdateUserCredentials");
					
					AdoESFSSO.W.AddIDParameter (cmd, "pPersonID", pUser.ID);
					AdoESFSSO.W.AddParameter (cmd, "pAlias", pUser.Alias);
					AdoESFSSO.W.AddParameter (cmd, "pPassword", pUser.Password);
					AdoESFSSO.W.AddAuditableEntityParamsUpdate (cmd, pUser);
				
					AdoESFSSO.W.Execute (cmd, dbClientMsg);
				}
							
				cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_UpdateUser");
				
				AdoESFSSO.W.AddParameter (cmd, "pPersonID", pUser.ID);
				AdoESFSSO.W.AddParameter (cmd, "pIsEnabled", pUser.IsEnabled);
				AdoESFSSO.W.AddIDParameter (cmd, "pSecurityQuestionID", pUser.SecurityQuestion.ID);
				AdoESFSSO.W.AddParameter (cmd, "pSecurityQuestionAnswer", pUser.SecurityQuestionAnswer);
				AdoESFSSO.W.AddAuditableEntityParamsUpdate (cmd, pUser);
				
				AdoESFSSO.W.Execute (cmd);
			}
			
			AsociateUserGroups (pUser);

			AsociateUserRights (pUser);
		}
		

		private static void AsociateUserRights (User pUser)
		{
			AdoUser.DeleteUserBasicReferenceValues (pUser, BasicReferenceTypes.UserRights);

			if (pUser.UserRights == null || pUser.UserRights.Count == 0) 
			{
				return;
			}
			
			foreach (BasicReference val in pUser.UserRights)
			{
				AdoUser.SetUserBasicReferenceValue (pUser, val);
			}
		}


		public static void DeleteBasicReferences (User pUser, BasicReferenceTypes pBasicReferenceType)
		{
			DeleteBasicReferences (pUser, (int) pBasicReferenceType);	
		}


		public static void DeleteBasicReferences (User pUser, int pBasicReferenceTypeID)
		{
			IDbCommand cmd;
		
		
			cmd = AdoESFSSO.W.CreateSPCommand ("[esf_sso].P_DeleteUser_BasicReferences");
		
			AdoESFSSO.W.AddIDParameter (cmd, "pUserPersonID", pUser.ID);
			AdoESFSSO.W.AddParameter (cmd, "pBasicReferenceTypeID", pBasicReferenceTypeID);
		
			AdoESFSSO.W.Execute (cmd);
		}


		private static void InsertBasicReference (User pUser, BasicReference pBR)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.W.CreateSPCommand ("[esf_sso].P_InsertUser_BasicReference");

			AdoESFSSO.W.AddIDParameter (cmd, "pUserPersonID", pUser.ID);
			AdoESFSSO.W.AddIDParameter (cmd, "pBasicReferenceID", pBR);
				
			AdoESFSSO.W.Execute (cmd);
		}


		public static void InsertBasicReferences (User pUser, List<BasicReference> pBRs, BasicReferenceTypes pBasicReferenceType)
		{
			InsertBasicReferences (pUser, pBRs, (int) pBasicReferenceType);
		}
		
		
		public static void InsertBasicReferences (User pUser, List<BasicReference> pBRs, int pBasicReferenceTypeID)
		{
			DeleteBasicReferences (pUser, pBasicReferenceTypeID);
			
			if (pBRs == null || pBRs.Count == 0) 
			{
				return;
			}

			foreach (BasicReference br in pBRs)
			{
				InsertBasicReference (pUser, br);
			}
		}

		
		private static void AsociateUserGroups (User pUser)
		{
			InsertBasicReferences (pUser, pUser.UserGroups, BasicReferenceTypes.UserGroup);
		}
		

		#region Menus

		public static int? RegisterUserMenu (UserMenuItem pUserMenuItem)
		{
			IDbCommand cmd;


			pUserMenuItem.Application.BasicReferenceType = new BasicReferenceType () { ID = (int) BasicReferenceTypes.Application };
			pUserMenuItem.Application = AdoBasicReference.Get (pUserMenuItem.Application);

			if (pUserMenuItem.ID == null)
			{
				cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_CreateAsignUserMenu");

				AdoESFSSO.W.AddOutParameter (cmd, "pID", DbType.Int32);

				AdoESFSSO.W.AddCodeParameter (cmd, "pApplicationCode", pUserMenuItem.Application);

				AdoESFSSO.W.AddAuditableEntityParamsCreation(cmd, pUserMenuItem);
			}
			else
			{
				cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_UpdateUserMenu");

				AdoESFSSO.W.AddIDParameter (cmd, "pID", pUserMenuItem.ID);

				AdoESFSSO.W.AddAuditableEntityParamsUpdate(cmd, pUserMenuItem);
			}

			AdoESFSSO.W.AddCodeParameter (cmd, "pParentCode", pUserMenuItem.Parent);
			AdoESFSSO.W.AddCodeParameter (cmd, "pCode", pUserMenuItem);
			AdoESFSSO.W.AddNameParameter (cmd, "pName", pUserMenuItem);
			AdoESFSSO.W.AddParameter (cmd, "pDescription", pUserMenuItem.Description);
			AdoESFSSO.W.AddParameter (cmd, "pExtra_I", pUserMenuItem.Extra_I);
			AdoESFSSO.W.AddParameter (cmd, "pURL", pUserMenuItem.URL);
			AdoESFSSO.W.AddParameter (cmd, "pIsEnabled", pUserMenuItem.IsEnabled);
			
			/*if (pUserMenuItem.Files != null && pUserMenuItem.Files.Count > 0)
			{
				AdoESFSSO.W.AddParameter (cmd, "pIconURL", pUserMenuItem.Files [0].URL);
				
				if (pUserMenuItem.Files.Count == 2)
				{
					AdoESFSSO.W.AddParameter (cmd, "pHeaderURL", pUserMenuItem.Files [1].URL);
				}
			}*/

			AdoESFSSO.W.Execute (cmd);

			return AdoESFSSO.W.GetParameterValue <int> (cmd, "pID");
		}


		public static void DeleteUserMenu (UserMenuItem pUserMenuItem)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_DeleteUserMenu");

			AdoESFSSO.W.AddIDParameter (cmd, "pID", pUserMenuItem);

			AdoESFSSO.W.Execute (cmd);
		}

		#endregion


		#endregion
	}
}
